package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;

/**
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/11/30 15:18
 */
public class Mob extends AbstractMapleStoryObject {

    {
        standImages = new Image[1];
        standImages[0] = ImageUtil.getImageByKey("mob01_stand");

        walkImages = new Image[4];
        for (int i = 0; i < walkImages.length; i++) {
            walkImages[i] = ImageUtil.getImageByKey("mob01_walk" + i);
        }

        dieImages = new Image[13];
        for (int i = 0; i < dieImages.length; i++) {
            dieImages[i] = ImageUtil.getImageByKey("mob01_die" + i);
        }

        hitImages = new Image[1];
        hitImages[0] = ImageUtil.getImageByKey("mob01_hit");
    }


    public Mob() {
        this.dir = Direction.LEFT;
        this.action = Action.WALK;
        this.width = standImages[0].getWidth(null);
        this.height = standImages[0].getHeight(null);
        this.HP = 10000;
        this.hp = HP;
        this.speed = 1;
    }

    public Mob(MapleStoryClient msc, int x, int y, int type) {
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.type = type;
    }

    private void confirmDie() {
        // 画完整个死亡状态的图片
        if (dieStep >= dieImages.length - 1) {
            dieStep = dieImages.length - 1;
            this.live = false;
        }
    }

    @Override
    public void move() {
        // 先判断方向和动作，使用随机生成
        if (this.live && r.nextInt(100) > 90 && this.action != Action.DIE) {
            // 根据自定义频率改变方向和动作状态
            this.dir = dirs[r.nextInt(dirs.length)];
            this.action = actions[r.nextInt(2)];
        }
        if (this.action == Action.WALK ) {
            if (this.dir == Direction.LEFT) {
                x -= speed;
            }
            if (this.dir == Direction.RIGHT) {
                this.x += speed;
            }
        }else if(this.action != Action.DIE){
            this.action = Action.STAND;
        }

        jumpDown(msc.grounds);
    }

    @Override
    public void draw(Graphics g) {
        // 确定死透了（图片组是否画完）
        confirmDie();

        if (!live) {
            // 死了就移除
            this.msc.mobs.remove(this);
            return;
        }
        g.drawRect(x + msc.background.x, y, width, height);

        // 避免出现int类型的最大（溢出）
        if (rate >= Integer.MAX_VALUE) {
            rate = 0;
        }
        if (walkStep > walkImages.length - 1) {
            walkStep = 0;
        }
        if (dieStep > dieImages.length - 1) {
            dieStep = 0;
        }

        switch (dir) {
            case LEFT:
                switch (action) {
                    case STAND:
                        g.drawImage(standImages[0], x + msc.background.x, y, null);
                        break;
                    case WALK:
                        g.drawImage(walkImages[walkStep], x+ msc.background.x, y-2, null);
                        break;
                    case DIE:
                        g.drawImage(dieImages[dieStep], x+ msc.background.x, y - 18, null);
                        break;
                    case HIT:
                        g.drawImage(hitImages[0], x+ msc.background.x, y - 19, null);
                        break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                // 转成2D带有transform属性的对象
                Graphics2D graphics2D = (Graphics2D) g;
                switch (action) {
                    case STAND:
                        graphics2D.drawImage(standImages[0], ImageUtil.flipHorizontal(standImages[0], x+ msc.background.x, y), null);
                        break;
                    case WALK:
                        graphics2D.drawImage(walkImages[walkStep], ImageUtil.flipHorizontal(walkImages[walkStep], x+ msc.background.x, y - 2), null);
                        break;
                    case DIE:
                        graphics2D.drawImage(dieImages[dieStep], ImageUtil.flipHorizontal(dieImages[dieStep], x+ msc.background.x, y - 18), null);
                        break;
                    case HIT:
                        graphics2D.drawImage(hitImages[0], ImageUtil.flipHorizontal(hitImages[0], x+ msc.background.x, y - 19), null);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;

        }
        if (rate++ % 2 == 0 && this.action == Action.DIE) {
            dieStep++;
        }
        if (rate % 5 == 0) {
            walkStep++;
        }
        // 画血条
        bloodBar.draw(g);

        move();
    }

    public BloodBar bloodBar = new BloodBar();

    class BloodBar extends AbstractMapleStoryObject {
        public BloodBar() {
            this.img = ImageUtil.getImageByKey("mob_blood");
            this.width = img.getWidth(null);
        }

        @Override
        public void draw(Graphics g) {
            // 内部类调用外部类同名成员变量
            /*g.drawRect(Mob.this.x,Mob.this.y - 15,Mob.this.width,10);
            Color c = g.getColor();
            g.setColor(Color.GREEN);
            g.fillRect(Mob.this.x + 1,Mob.this.y - 14, (int) (Mob.this.hp / Mob.this.HP * (Mob.this.width - 1)),9);
            g.setColor(c);*/

            // 图片
            // Mob.this.width / this.width : 画多少个
            // Mob.this.hp/Mob.this.HP: 血的百分比
            int num = (int) ((Mob.this.width / this.width) * (Mob.this.hp / Mob.this.HP));
            for (int i = 0; i < num; i++) {
                g.drawImage(img, Mob.this.x + Mob.this.msc.background.x + (i * this.width), Mob.this.y - 15, null);
            }

        }
    }


}
